package com.woniuxy.third.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.third.entity.*;
import com.woniuxy.third.mapper.*;
import com.woniuxy.third.model.BasePageInfo;
import com.woniuxy.third.model.CourseQuery;
import com.woniuxy.third.service.CourseService;
import com.woniuxy.third.utils.GetUserUtil;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

@Service
public class  CourseServiceImpl implements CourseService {
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private ChapterMapper chapterMapper;
    @Resource
    private SectionMapper sectionMapper;
    @Resource
    private ReferenceMapper referenceMapper;
    @Resource
    private AssessmentMapper assessmentMapper;


    @Override
    public Course findCourseByid(int id) {
        return courseMapper.findCourseByid(id);
    }

    @Override
    public List<Course> findAllCourse() {
        return courseMapper.findAllCourse();
    }

    @Override
    public boolean addCourse(Course course) {
        return courseMapper.addCourse(course);
    }

    @Override
    public List<Chapter> findChaptersByCourseId(int id) {
        List<Chapter> chapters = chapterMapper.findChaptersByCourseid(id);
        for (Chapter chapter:chapters) {
            List<Section> sections = sectionMapper.findSectionsByChapterid(chapter.getId());
            chapter.setSections(sections);
        }
        return chapters;
    }

    @Override
    public Course findCourseByUser(User user) {
        Course course = courseMapper.findCourseByid(user.getId());
        return course;
    }

    @Override
    public PageInfo<Course> getCourseFrontlist(CourseQuery courseQuery) {
        try {
            Map<String, Object> map = new HashMap();
            map.put("obj", courseQuery);
            PageHelper.startPage(courseQuery.getPage(), courseQuery.getPageSize());
            List<Course> list = courseMapper.findCourseByCondition(map);
            PageInfo<Course> pageInfo=new PageInfo<Course>(list);
            return pageInfo;
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public List<Course> findCourseByUid(int uid) {
        List<Course> courses = courseMapper.findCourseByUid(uid);
        return courses;
    }

    @Override
    public int updateCourse(Course course) {
        return courseMapper.updateCourse(course);
    }

    public List<Course> findCollectionCourseByUid(int uid){
        List<Course> collectionCourses = courseMapper.findCollectionCourseByUid(uid);
        return collectionCourses;
    }

    @Override
    public List<Course> findCourseBytid(int id) {
        return courseMapper.findCourseBytid(id);
    }

    @Override
    public Course findCourseByName(String name,int id) {
        return courseMapper.findCourseByName(name,id);
    }

    @Override
    public List<Section> findSectionsByChaptersId(int chapterid) {
        List<Section> sections = sectionMapper.findSectionsByChapterid(chapterid);
        return sections;
    }

    @Override
    public List<Reference> findReferencesByCourceId(int id) {
        List<Reference> references = referenceMapper.findReferencesByCourceId(id);
        return references;
    }

    @Override
    public Assessment findAssessmentById(int id) {
        return  assessmentMapper.findAssessmentById(id);
    }

}
